<?php
require_once __DIR__ . '/../../app/config/config.php';

function symbolKline($symbol, $period = '30min', $size = 2)
{
    $access_key = '77753ea4-583ea19e-c9fb61a5-9036b';
    $url = 'https://api.huobipro.com/market/history/kline';
    $query = [
        'period' => $period,
        'size' => $size,
        'symbol' => $symbol,
        'AccessKeyId' => $access_key,
    ];
    $query = http_build_query($query, '', '&');
    $url = $url . '?' . $query;

    try {

        list($head, $body) = httpGet($url);
//        printf("%s\r\n%s\r\n", $url, $body);
        $data = json_decode($body, true);

        printf("%s\r\n%s\r\n", $url, $body);

        $default = array(
            'ch' => $data['ch'],
            'ts' => $data['ts'],
            'tick' => $data['data'][0],
        );

    } catch (RuntimeException $e) {
        printf("error\t%s\r\n%s\r\n", $url, $e->getMessage());
        return 'error';
    }

//    $url = getUrlForKline($data['ch']);
    $url = "http://47.94.163.169/kline_pre5min";

    if ($url == null) {
        return 'kline url is null.';
    }

    try {
        list($head, $body) = httpRequest($url, json_encode($default), 5);
        printf("%s\r\n%s\r\n", $url, $body);

        echo PHP_EOL;
    } catch (RuntimeException $e) {
        printf("error\t%s\r\n%s\r\n", $url, $e->getMessage());
    }

    return $body;
}

function httpGet($url, $timeout = 5000)
{
    if (false === $curl = curl_init()) {
        throw new RuntimeException('Unable to create a cURL handle.');
    }

    $http_user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36';

    curl_setopt($curl, CURLOPT_URL, $url);

    curl_setopt($curl, CURLOPT_USERAGENT, $http_user_agent);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, true);

    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($curl, CURLOPT_TIMEOUT, 10);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl, CURLOPT_MAXREDIRS, 3);

    $response = curl_exec($curl);

    if (false === $response) {
        $error = curl_error($curl);
        curl_close($curl);
        throw new RuntimeException(sprintf('An error occurred: %s.', $error));
    }

    $headersSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
    $headers = substr($response, 0, $headersSize);
    $body = substr($response, $headersSize);

    $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    curl_close($curl);

    if (400 == $statusCode) {
        throw new RuntimeException('400 error.');
    }

    if (200 != $statusCode) {
        throw new RuntimeException(sprintf('The web service failed for an unknown reason (HTTP %s).', $statusCode));
    }

    return array($headers, $body);
}

function main($period = '30min', $size = 2)
{
    global $SYMBOL_FUTURES;

    $i = $sum = 0;
    foreach (array_unique($SYMBOL_FUTURES) as $symbol) {
        $message = symbolKline($symbol, $period, $size);
        usleep(150);
        $sum++;
    }
    return $sum;
}


$params = getopt('k:s::');

if (isset($params['k']) &&
    in_array($params['k'], ['1min', '5min', '15min', '30min', '60min', '1day', '1mon', '1week', '1year'])) {
    if (isset($params['s'])) {
        $message = symbolKline($params['s'], $params['k']);
    } else {
        $message = main($params['k']);
    }
} else {
    $message = <<<EOF
s: btcusdt,bchusdt...
k: 1min, 5min, 15min, 30min, 60min, 1day, 1mon, 1week, 1year
EOF;

}
echo $message;
